package com.example.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.common.PageRequest;
import com.example.parking.common.Result;
import com.example.parking.entity.Vehicle;
import com.example.parking.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vehicle")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    @PostMapping
    public Result<Vehicle> create(@RequestBody Vehicle vehicle) {
        vehicleService.save(vehicle);
        return Result.success(vehicle);
    }

    @PutMapping("/{id}")
    public Result<Vehicle> update(@PathVariable Long id, @RequestBody Vehicle vehicle) {
        vehicle.setId(id);
        vehicleService.updateById(vehicle);
        return Result.success(vehicle);
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        vehicleService.removeById(id);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    public Result<Vehicle> getById(@PathVariable Long id) {
        return Result.success(vehicleService.getById(id));
    }

    @GetMapping("/page")
    public Result<Page<Vehicle>> page(PageRequest pageRequest, Vehicle vehicle) {
        Page<Vehicle> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        LambdaQueryWrapper<Vehicle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(vehicle.getUserId() != null, Vehicle::getUserId, vehicle.getUserId())
                .like(vehicle.getPlateNumber() != null, Vehicle::getPlateNumber, vehicle.getPlateNumber())
                .eq(vehicle.getVehicleType() != null, Vehicle::getVehicleType, vehicle.getVehicleType())
                .like(vehicle.getBrand() != null, Vehicle::getBrand, vehicle.getBrand())
                .like(vehicle.getModel() != null, Vehicle::getModel, vehicle.getModel())
                .like(vehicle.getColor() != null, Vehicle::getColor, vehicle.getColor())
                .eq(vehicle.getStatus() != null, Vehicle::getStatus, vehicle.getStatus());
        return Result.success(vehicleService.page(page, wrapper));
    }

    @GetMapping("/listByUserId")
    public Result<List<Vehicle>> listByUserId(@RequestParam Long userId) {
        try {
            LambdaQueryWrapper<Vehicle> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Vehicle::getUserId, userId);
            List<Vehicle> vehicles = vehicleService.list(wrapper);
            System.out.println("Fetched vehicles by userId " + userId + ": " + vehicles.size());
            return Result.success(vehicles);
        } catch (Exception e) {
            System.err.println("Error fetching vehicle list by userId: " + e.getMessage());
            e.printStackTrace();
            return Result.error("获取用户车辆列表失败");
        }
    }

    @GetMapping("/list")
    public Result<List<Vehicle>> listAll() {
        try {
            List<Vehicle> vehicles = vehicleService.list();
            System.out.println("Fetched vehicles: " + vehicles.size());
            return Result.success(vehicles);
        } catch (Exception e) {
            System.err.println("Error fetching vehicle list: " + e.getMessage());
            e.printStackTrace();
            return Result.error("获取车辆列表失败");
        }
    }
} 